class Solution {//leetcode692——前k个高频单词
private:
    class comp{
    public:
        bool operator()(pair<string,int>&lhs,pair<string,int>&rhs){
            if(lhs.second==rhs.second){
                return lhs.first<rhs.first;
            }
            return lhs.second>rhs.second;
        }
    };
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        unordered_map<string,int> hash;
        int n=words.size();
        for(auto& str:words){
            hash[str]++;
        }
        priority_queue<pair<string,int>,vector<pair<string,int>>,comp> q;
        for(auto &[x,y]:hash){
            q.push({x,y});
            if(q.size()>k) q.pop();
        }
        vector<string> ret(k);
        for(int i=k-1;i>=0;i--){
            ret[i]=q.top().first;
            q.pop();
        }
        return ret;
    }
};